<?php

if ( ! class_exists( 'WP_List_Table' ) )
	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

class P2C_Platform_List_Table extends WP_List_Table {

	function __construct() {
		parent::__construct( array(
			'singular' => 'platform',
			'plural' => 'platforms',
			'ajax' => false ) );
	}

	function prepare_items() {
		$columns = $this->get_columns();
		$per_page = 10;

		$hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        
		$args = array(
			'posts_per_page' => $per_page,
			'orderby' => 'name',
			'order' => 'ASC',
			'offset' => ( $this->get_pagenum() - 1 ) * $per_page );
        
		if ( ! empty( $_REQUEST['orderby'] ) ) {
            $args['orderby'] = $_REQUEST['orderby'];
		}

		if ( ! empty( $_REQUEST['order'] ) ) {
			if ( 'asc' == strtolower( $_REQUEST['order'] ) )
				$args['order'] = 'ASC';
			elseif ( 'desc' == strtolower( $_REQUEST['order'] ) )
				$args['order'] = 'DESC';
		}

		$this->items = P2C_Platform::find( $args );

		$total_items = P2C_Platform::rowcount();
		$total_pages = ceil( $total_items / $per_page );
        
		$this->set_pagination_args( array(
			'total_items' => $total_items,
			'total_pages' => $total_pages,
			'per_page' => $per_page ) );
	}

	function get_columns() {
        $columns = array(
			'cb' => '<input type="checkbox" />',
			'name' => __( 'Platform', 'p2c_admin' ),
			'url' => __( 'URL', 'p2c_admin' ),
			'active' => __( 'Active', 'p2c_admin' ),
            'projects' => __( 'Projects', 'p2c_admin' ),
            'created_by' => __( 'Created By', 'p2c_admin' ),
            'create_date' => __( 'Created', 'p2c_admin' ),
            'update_date' => __( 'Updated', 'p2c_admin' ),
            'parse_date' => __( 'Parsed', 'p2c_admin' ));

		return $columns;
	}

	function get_sortable_columns() {
		$columns = array(
			'name' => array( 'name', true ),
			'active' => array( 'active', false ),
            'created_by' => array( 'created_by', false ),
            'create_date' => array( 'create_date', false ),
            'update_date' => array( 'update_date', false ),
            'parse_date' => array( 'parse_date', false ),
            'projects' => array( 'projects', false ));

		return $columns;
	}

	function get_bulk_actions() {
		$actions = array();
			//'parse' => __( 'Parse', 'p2c_admin' ) );

		return $actions;
	}

	function column_default( $item, $column_name ) {
		return '';
    }

	function column_cb( $item ) {
		return sprintf(
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
			$this->_args['singular'],
			$item->id );
	}

	function column_name( $item ) {
		$url = admin_url( 'admin.php?page=p2cpf&pid=' . absint( $item->id ) );
		$edit_link = add_query_arg( array( 'action' => 'edit' ), $url );
        $parse_link = add_query_arg( array( 'action' => 'parse' ), $url );

        if($item->active == 1) {
            $actions = array(
                'edit' => '<a href="' . $edit_link . '">' . __( 'Edit', 'p2c_admin' ) . '</a>',
                'parse' => '<a href="' . $parse_link . '">' . __( 'Parse', 'p2c_admin' ) . '</a>');
        } else {
            $actions = array(
			 'edit' => '<a href="' . $edit_link . '">' . __( 'Edit', 'p2c_admin' ) . '</a>');
        }

		$a = sprintf( '<a class="row-title" href="%1$s" title="%2$s">%3$s</a>',
			$edit_link,
			esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;', 'p2c_admin' ), $item->name ) ),
			esc_html( $item->name ) );

		return '<strong>' . $a . '</strong> ' . $this->row_actions( $actions );
    }

	function column_url( $item ) {
        $url = esc_html( $item->url );
        $name = esc_html( $item->name );
        
        $a = sprintf( '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>',
			$url,
			$name ,
			$url );
        
		return $a;
    }
    
    function column_projects( $item ) {
        return $item->projects;
	}
    
    function column_created_by( $item ) {
        return $item->created_by;
	}
    
    function column_create_date( $item ) {
        return $item->create_date;
	}
    
    function column_update_date( $item ) {
        return $item->update_date;
	}
    
    function column_parse_date( $item ) {
        return $item->parse_date;
	}

	function column_active( $item ) {
        if($item->active == 1)
            return "Yes";
        else
            return "No";
    }
}

?>